package com.my.simple.service.auth;

import com.my.simple.common.enums.SecurityEnum;
import com.my.simple.common.util.EHCacheUtils;
import com.my.simple.entity.auth.Operation;
import com.my.simple.service.bo.UserRoleBO;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Service
public class PermissionService {

    @Autowired
    private OperationService operationService;

    public Boolean isPermissionUrl(String pathCode) {
        boolean isNeedPermission = false;
        List<Operation> permissionOperationList = operationService.getNeedPermissionOperation();

        if (StringUtils.isBlank(pathCode)) {
            throw new IllegalArgumentException("url is blank");
        }

        if (permissionOperationList != null && !permissionOperationList.isEmpty()) {
            for (Operation item : permissionOperationList) {
                if (isMatchPermissionCode(pathCode, item.getOperationCode())) {
                    isNeedPermission = true;
                    break;
                }
            }
        }

        return isNeedPermission;
    }

    public UserRoleBO checkLoginInfo(HttpServletRequest request) {
        UserRoleBO userRoleBO = null;
        String token = request.getHeader(SecurityEnum.MyToken.name());

        if (StringUtils.isNotBlank(token)) {
            Object obj = EHCacheUtils.get(token);
            if (obj != null) {
                userRoleBO = (UserRoleBO) obj;
            }
        }
        return userRoleBO;
    }

    public Boolean hasPermissionCheck(String permissionCode, List<Operation> userOperation) {
        Boolean hasPermission = false;

        if (StringUtils.isNotBlank(permissionCode) && userOperation != null && !userOperation.isEmpty()) {
            for (Operation item : userOperation) {
                if (isMatchPermissionCode(permissionCode, item.getOperationCode())) {
                    hasPermission = true;
                    break;
                }
            }
        }
        return hasPermission;
    }

    public String convertUrlToPermissionCode(String url) {
        String pathCode = null;
        //转为小写
        //将url中 \, / , 等字符转为_ 下划线
        //将url 变为两级结构 controller_action, 实现对子url 判断
        //去除两头空格

        if (StringUtils.isNotBlank(url)) {
            url = url.toLowerCase();
            url = url.replace("\\", "_").trim();
            url = url.replace("/", "_");
            url = StringUtils.strip(url, "_").trim();
            String[] subUrlList = url.split("_");
            StringBuilder sbUrl = new StringBuilder();

            if (subUrlList != null && subUrlList.length > 0) {
                for (int j = 0; j < subUrlList.length; j++) {
                    if (j < 2) {
                        sbUrl.append(subUrlList[j]);
                        sbUrl.append("_");
                    } else {
                        break;
                    }
                }
            }

            pathCode = sbUrl.toString();
            pathCode = StringUtils.strip(pathCode, "_").trim();
        }
        return pathCode;
    }

    public Boolean isMatchPermissionCode(String sourceCode, String operationCode) {
        Boolean isMatch = false;
        if (StringUtils.isBlank(sourceCode)) {
            return false;
        }
        if (StringUtils.isBlank(operationCode)) {
            return true;
        }

        sourceCode = sourceCode.trim();
        operationCode = operationCode.trim();
        isMatch = sourceCode.equalsIgnoreCase(operationCode);
        return isMatch;
    }
}
